/******FIGURE III: HETEROGENEOUS EFFECTS***********/


/****RUN ANALYSIS***************/
use ../data/main.dta, clear
keep if sample_main

global fixed_effects "cb pop_dec#elec_id CBG#elec_id"

//HETEROGENEITY BY VOTER RACE
foreach y in reg vote {
    foreach r in black hisp whiteasian {
		sum `y'_`r' if tau==-1
		local m = round(r(mean), .01)
	    reghdfe `y'_`r' treat_post , absorb( $fixed_effects ) $tol  cluster(CBG) nocons
		parmest, label saving (../estimates/voterrace_`y'_`r', replace)
		outreg2 using ../output/table_heterogeneity.xls, append stats(coef se) ctitle(`y') dec(3) alpha(.01, .05, .10) addtext(group, `r', mean, `m') symbol(***, **, *) excel 
	}


//HETEROGENEITY BY VICTIM RACE
	foreach r in blackhispanic whiteasian {
		sum `y'_`r' if tau==-1
		local m = round(r(mean), .01)
		reghdfe `y'_`r' tp_blackhispanic tp_whiteasian, absorb( $fixed_effects ) $tol  cluster(CBG) nocons
		parmest, label saving (../estimates/victimrace_`y'_`r', replace)
		outreg2 using ../output/table_heterogeneity.xls, append stats(coef se) ctitle(`y') dec(3) alpha(.01, .05, .10) addtext(group, `r', mean, `m') symbol(***, **, *) excel 
	}


//HETEROGENEITY BY AGE
	foreach r in 1834 3554 55pl {
		sum `y'_`r' if tau==-1
		local m = round(r(mean), .01)
			reghdfe `y'_`r' treat_post , absorb( $fixed_effects ) $tol  cluster(CBG) nocons
			parmest, label saving (../estimates/age_`y'_`r', replace)
		outreg2 using ../output/table_heterogeneity.xls, append stats(coef se) ctitle(`y') dec(3) alpha(.01, .05, .10) addtext(group, `r', mean, `m') symbol(***, **, *) excel 
	}


//HETEROGENEITY YEARS REGISTERED
	foreach r in 0to3 4to7 8to11 12to15  {
		sum `y'_`r' if tau==-1
		local m = round(r(mean), .01)
		reghdfe `y'_`r' treat_post , absorb( $fixed_effects ) $tol  cluster(CBG) nocons
		parmest, label saving (../estimates/regcycle_`y'_`r', replace)
		outreg2 using ../output/table_heterogeneity.xls, append stats(coef se) ctitle(`y') dec(3) alpha(.01, .05, .10) addtext(group, `r', mean, `m') symbol(***, **, *) excel 

	}


//HETEROGENEITY BY POLITICAL AFFILIATION

foreach r in dem rep ind {
		sum `y'_`r' if tau==-1
		local m = round(r(mean), .01)
	    reghdfe `y'_`r' treat_post , absorb( $fixed_effects ) $tol cluster(CBG) nocons
		parmest, label saving (../estimates/party_`y'_`r', replace)
		outreg2 using ../output/table_heterogeneity.xls, append stats(coef se) ctitle(`y') dec(3) alpha(.01, .05, .10) addtext(group, `r', mean, `m') symbol(***, **, *) excel 
}


//HETEROGENEITY BY WHETHER VICTIM IS A RESIDENT
		sum `y' if tau==-1
		local m = round(r(mean), .01)
reghdfe `y' tp_resident tp_not_resident , absorb( $fixed_effects ) $tol cluster(CBG) nocons
parmest, label saving (../estimates/resident_`y', replace)
outreg2 using ../output/table_heterogeneity.xls, append stats(coef se) ctitle(`y') dec(3) alpha(.01, .05, .10) addtext(group, all, mean, `m') symbol(***, **, *) excel 
}


/******CREATE FIGURES**********/
local color1 = "navy"
local color2 = "maroon*0.65"
local symbol1 = "circle"
local symbol2 = "diamond"
local tsize = "large"
local labsize = "medlarge"
local titlesize = "vlarge"

local ymax1 = 5
local ymin1 = -1
local ymax1a = 5.6
local ymin1a = -1.6

local ymax3 = 12
local ymin3 = -3
local ymax3a = 13
local ymin3a = -4

local increment1 = 1
local increment3 = 3



//HETEROGENEITY BY VOTER RACE
clear
gen race = ""
gen out = ""
foreach y in reg vote {
foreach r in black hisp whiteasian {
    append using ../estimates/voterrace_`y'_`r'
	replace race = "`r'" if race==""
	replace out = "`y'" if out==""
}
}

gen reg = 1 if out=="reg"

gen rnum = 1 if race=="black"
replace rnum = 2 if race=="hisp"
replace rnum = 3 if race=="whiteasian"

replace rnum = rnum -.15 if reg==1
replace rnum = rnum +.15 if reg==.

graph twoway (scatter estimate rnum if reg==1, msymbol(`symbol1') mcolor(`color1')) (rcap min95 max95 rnum if reg==1, lcolor(`color1')) ///
	(scatter estimate rnum if reg==., msymbol(`symbol2') mcolor(`color2')) (rcap min95 max95 rnum if reg==., lcolor(`color2')), ///
	yline(0, lcolor(gs5)) ///
	ylabel(`ymin1'(`increment1')`ymax1', grid labsize(`labsize')) yscale(range(`ymin1a' `ymax1a')) ///
	xlabel(1 "Black" 2 "Hispanic" 3 "White/Asian", labsize(`tsize')) xscale(range(0.7 3.3)) ///
	ytitle("Counts", size(`tsize')) ///
	xtitle("") ///
	legend(order(1 3) label(1 "Registrations") label(3 "Votes") size(`tsize')) ///
	plotregion(fcolor(white)) graphregion(fcolor(white) lwidth(large) lcolor(white)) bgcolor(white)
	graph export ../output/fig3_voterrace.png, replace
	graph export ../output/fig3_voterrace.eps, replace

//HETEROGENEITY BY VICTIM RACE
clear
gen race = ""
gen out = ""
foreach y in reg vote {
foreach r in blackhispanic whiteasian {
    append using ../estimates/victimrace_`y'_`r'
	replace race = "`r'" if race==""
	replace out = "`y'" if out==""
}
}

gen reg = 1 if out=="reg"

gen rnum = 1 if race=="blackhispanic" & parm=="tp_blackhispanic"
replace rnum = 2 if race=="blackhispanic" & parm=="tp_whiteasian"
replace rnum = 3 if race=="whiteasian" & parm=="tp_blackhispanic"
replace rnum = 4 if race=="whiteasian" & parm=="tp_whiteasian"

replace rnum = rnum -.15 if reg==1
replace rnum = rnum +.15 if reg==.

graph twoway (scatter estimate rnum if reg==1, msymbol(`symbol1') mcolor(`color1')) (rcap min95 max95 rnum if reg==1, lcolor(`color1')) ///
	(scatter estimate rnum if reg==., msymbol(`symbol2') mcolor(`color2')) (rcap min95 max95 rnum if reg==., lcolor(`color2')), ///
	yline(0, lcolor(gs5)) xline(2.5, lcolor(gs12) lstyle(dash)) ///
	ylabel(`ymin1'(`increment1')`ymax1', grid labsize(`labsize')) yscale(range(`ymin1a' `ymax1a')) ///
	xlabel(1 `" "Black/Hisp" "Killings" "' 2 `" "White/Asian" "Killings" "' 3 `" "Black/Hisp" "Killings" "' 4 `" "White/Asian" "Killings" "', labsize(`tsize')) xscale(range(0.5 4.5)) ///
	xtitle("") ///
	ytitle("Counts", size(`tsize')) ///
	legend(order(1 3) label(1 "Registrations") label(3 "Votes") size(`tsize')) ///
	text(5.7 1.55 "Black/Hisp Voters",  size(`tsize') place(c)) text(5.7 3.6 "White/Asian Voters",  size(`tsize') place(c)) ///
	plotregion(fcolor(white)) graphregion(fcolor(white) lwidth(large) lcolor(white)) bgcolor(white)
	graph export ../output/fig3_victimrace.png, replace
	graph export ../output/fig3_victimrace.eps, replace


//HETEROGENEITY BY AGE
clear
gen age = ""
gen out = ""
foreach y in reg vote {
foreach r in 1834 3554 55pl {
    append using ../estimates/age_`y'_`r'
	replace age = "`r'" if age==""
	replace out = "`y'" if out==""
}
}

gen reg = 1 if out=="reg"

gen rnum = 1 if age=="1834"
replace rnum = 2 if age=="3554"
replace rnum = 3 if age=="55pl"

replace rnum = rnum -.15 if reg==1
replace rnum = rnum +.15 if reg==.

graph twoway (scatter estimate rnum if reg==1, msymbol(`symbol1') mcolor(`color1')) (rcap min95 max95 rnum if reg==1, lcolor(`color1')) ///
	(scatter estimate rnum if reg==., msymbol(`symbol2') mcolor(`color2')) (rcap min95 max95 rnum if reg==., lcolor(`color2')), ///
	yline(0, lcolor(gs5)) ///
	ylabel(`ymin1'(`increment1')`ymax1', grid labsize(`labsize')) yscale(range(`ymin1a' `ymax1a')) ///
	xlabel(1 "18-34 years" 2 "35-54 years" 3 "55+ years", labsize(`tsize')) xscale(range(0.7 3.3)) ///
	ytitle("Counts", size(`tsize')) ///
	xtitle("") ///
	legend(order(1 3) label(1 "Registrations") label(3 "Votes") size(`tsize')) ///
	plotregion(fcolor(white)) graphregion(fcolor(white) lwidth(large) lcolor(white)) bgcolor(white)
	graph export ../output/fig3_age.png, replace
	graph export ../output/fig3_age.eps, replace

	

//HETEROGENEITY BY YEARS REGISTERED
clear
gen cycle = ""
gen out = ""
foreach y in reg vote {
foreach r in 0to3 4to7 8to11 12to15 {
    append using ../estimates/regcycle_`y'_`r'
	replace cycle = "`r'" if cycle==""
	replace out = "`y'" if out==""
}
}

gen reg = 1 if out=="reg"

gen rnum = 1 if cycle=="0to3"
replace rnum = 2 if cycle=="4to7"
replace rnum = 3 if cycle=="8to11"
replace rnum = 4 if cycle=="12to15"

replace rnum = rnum -.15 if reg==1
replace rnum = rnum +.15 if reg==.

graph twoway (scatter estimate rnum if reg==1, msymbol(`symbol1') mcolor(`color1')) (rcap min95 max95 rnum if reg==1, lcolor(`color1')) ///
	(scatter estimate rnum if reg==., msymbol(`symbol2') mcolor(`color2')) (rcap min95 max95 rnum if reg==., lcolor(`color2')), ///
	yline(0, lcolor(gs5)) ///
	ylabel(`ymin1'(`increment1')`ymax1', grid labsize(`labsize')) yscale(range(`ymin1a' `ymax1a')) ///
	xlabel(1 "0-3 years"  2 "4-7 years" 3 "8-11 years" 4 "12-15 years", labsize(`tsize')) xscale(range(.7 4.3)) ///
	ytitle("Counts", size(`tsize')) ///
	xtitle("") ///
	legend(order(1 3) label(1 "Registrations") label(3 "Votes") size(`tsize')) ///
	plotregion(fcolor(white)) graphregion(fcolor(white) lwidth(large) lcolor(white)) bgcolor(white)
	graph export ../output/fig3_regcycle.png, replace
	graph export ../output/fig3_regcycle.eps, replace
	
//HETEROGENEITY BY POLITICAL AFFILIATION
clear
gen party = ""
gen out = ""
foreach y in reg vote {
foreach r in dem rep ind {
    append using ../estimates/party_`y'_`r'
	replace party = "`r'" if party==""
	replace out = "`y'" if out==""
}
}

gen reg = 1 if out=="reg"

gen rnum = 1 if party=="dem"
replace rnum = 2 if party=="ind"
replace rnum = 3 if party=="rep"

replace rnum = rnum -.15 if reg==1
replace rnum = rnum +.15 if reg==.

graph twoway (scatter estimate rnum if reg==1, msymbol(`symbol1') mcolor(`color1')) (rcap min95 max95 rnum if reg==1, lcolor(`color1')) ///
	(scatter estimate rnum if reg==., msymbol(`symbol2') mcolor(`color2')) (rcap min95 max95 rnum if reg==., lcolor(`color2')), ///
	yline(0, lcolor(gs5)) ///
	ylabel(`ymin1'(`increment1')`ymax1', grid labsize(`labsize')) yscale(range(`ymin1a' `ymax1a')) ///
	xlabel(1 "Democrat" 2 "Independent" 3 "Republican", labsize(`tsize')) xscale(range(0.7 3.3)) ///
	ytitle("Counts", size(`tsize')) ///
	xtitle("") ///
	legend(order(1 3) label(1 "Registrations") label(3 "Votes") size(`tsize')) ///
	plotregion(fcolor(white)) graphregion(fcolor(white) lwidth(large) lcolor(white)) bgcolor(white)
	graph export ../output/fig3_party.png, replace
	graph export ../output/fig3_party.eps, replace

	
//HETEROGEEITY BY INCIDENT LOCATION
clear
gen type = ""
gen out = ""
foreach y in reg vote {
foreach r in resident {
    append using ../estimates/`r'_`y'
	replace type = "`r'" if type==""
	replace out = "`y'" if out==""
}
}

gen reg = 1 if out=="reg"

gen rnum = 1 if parm=="tp_resident"
replace rnum = 2 if parm=="tp_not_resident"

replace rnum = rnum -.15 if reg==1
replace rnum = rnum +.15 if reg==.

graph twoway (scatter estimate rnum if reg==1, msymbol(`symbol1') mcolor(`color1')) (rcap min95 max95 rnum if reg==1, lcolor(`color1')) ///
	(scatter estimate rnum if reg==., msymbol(`symbol2') mcolor(`color2')) (rcap min95 max95 rnum if reg==., lcolor(`color2')), ///
	yline(0, lcolor(gs5)) ///
	ylabel(`ymin3'(`increment3')`ymax3', grid labsize(`labsize')) yscale(range(`ymin3a' `ymax3a')) ///
	 xline(2.5, lcolor(gs12) lstyle(dash)) ///
	xlabel(1 "Near Victim Home" 2 "Not Near Victim Home", labsize(`tsize')) xscale(range(0.7 2.3)) ///
	ytitle("Counts", size(`tsize')) ///
	xtitle("") ///
	legend(order(1 3) label(1 "Registrations") label(3 "Votes") size(`tsize')) ///
	plotregion(fcolor(white)) graphregion(fcolor(white) lwidth(large) lcolor(white)) bgcolor(white)
	graph export ../output/fig3_residence.png, replace
	graph export ../output/fig3_residence.eps, replace
